#include "at32f421_usart.h"
#include "fldrv_uart.h"
#include <string.h>

#define UART1_BAUDRATE	115200
#define UART2_BAUDRATE	115200

//uint8_t iap_flag = IAP_REV_FLAG_NO;

fldrv_uart_rx_t fldrv_uart1_rx;
fldrv_uart_rx_t fldrv_uart2_rx,fldrv_uart2_rx_back;

/**
  * @brief  init usart.
  * @param  none
  * @retval none
  */
void fldrv_uart_init(void)
{
  gpio_init_type gpio_init_struct;
  /* enable the usart and it's io clock */
  crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
	crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, TRUE);
  crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
	
  /* set default parameter */
  gpio_default_para_init(&gpio_init_struct);
	
  /* configure the usart1_tx/rx pa9/pa10 */
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  gpio_init_struct.gpio_pins = GPIO_PINS_2 | GPIO_PINS_3 | GPIO_PINS_9 | GPIO_PINS_10;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(GPIOA, &gpio_init_struct);

	gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE2, GPIO_MUX_1);	//UART2 -> SLM320
  gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE3, GPIO_MUX_1);

  gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE9, GPIO_MUX_1);	//UART1 -> device
  gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE10, GPIO_MUX_1);

  /*configure usart nvic interrupt */
  nvic_irq_enable(USART1_IRQn, 3, 0);
	nvic_irq_enable(USART2_IRQn, 2, 1);

  /*configure usart param*/
  usart_init(USART1, UART1_BAUDRATE, USART_DATA_8BITS, USART_STOP_1_BIT);
  usart_transmitter_enable(USART1, TRUE);
  usart_receiver_enable(USART1, TRUE);
  usart_interrupt_enable(USART1, USART_RDBF_INT, TRUE);
  usart_enable(USART1, TRUE);
	
	
	usart_init(USART2, UART2_BAUDRATE, USART_DATA_8BITS, USART_STOP_1_BIT);
	usart_transmitter_enable(USART2, TRUE);
	usart_receiver_enable(USART2, TRUE);
	usart_interrupt_enable(USART2, USART_RDBF_INT, TRUE);
  usart_enable(USART2, TRUE);
}

/**
  * @brief  usart1 interrupt handler.
  * @param  none
  * @retval none
  */
void USART1_IRQHandler(void)
{
  uint16_t reval;
  if(usart_flag_get(USART1, USART_RDBF_FLAG) == RESET)
		return;
  reval = usart_data_receive(USART1);
	if(fldrv_uart1_rx.u16UartRxLen < UART1_RXBUF_SIZE)
	{
		fldrv_uart1_rx.u8UartRxBuf[fldrv_uart1_rx.u16UartRxLen] = reval;
		fldrv_uart1_rx.u16UartRxLen++;
		fldrv_uart1_rx.u8UartRcvTimeOut = FALSE;
	}
}

fldrv_uart_rx_t fldrv_uart1_rcv_time_out_check()
{
	fldrv_uart_rx_t buf;
	memset(&buf, 0x00, sizeof(buf));
	if(fldrv_uart1_rx.u8UartRcvTimeOut < UART_TIME_OUT)	fldrv_uart1_rx.u8UartRcvTimeOut++;
	else if(fldrv_uart1_rx.u16UartRxLen)
	{
		buf = fldrv_uart1_rx;
		memset(&fldrv_uart1_rx, 0x00, sizeof(fldrv_uart1_rx));
		return buf;
	}
	return buf;
}
/**
  * @brief  usart1 interrupt handler.
  * @param  none
  * @retval none
  */
void USART2_IRQHandler(void)
{
  uint8_t reval;
  if(usart_flag_get(USART2, USART_RDBF_FLAG) == RESET)
  return;
  reval = usart_data_receive(USART2);
	if(fldrv_uart2_rx.u16UartRxLen < UART2_RXBUF_SIZE)
	{
		fldrv_uart2_rx.u8UartRxBuf[fldrv_uart2_rx.u16UartRxLen] = reval;
		fldrv_uart2_rx.u16UartRxLen++;
		fldrv_uart2_rx.u8UartRcvTimeOut = FALSE;
	}
}

fldrv_uart_rx_t fldrv_uart2_rcv_time_out_check()
{
	fldrv_uart_rx_t buf;
	memset(&buf, 0x00, sizeof(buf));
	if(fldrv_uart2_rx.u8UartRcvTimeOut < UART_TIME_OUT)	fldrv_uart2_rx.u8UartRcvTimeOut++;
	else if(fldrv_uart2_rx.u16UartRxLen)
	{
		fldrv_uart2_rx_back = fldrv_uart2_rx;
		memset(&fldrv_uart2_rx, 0x00, sizeof(fldrv_uart2_rx));
		return fldrv_uart2_rx_back;
	}
	return buf;
}


void fldrv_uart_send_buf(char* data,uint8_t len)
{
	uint8_t i;
	for (i=0; i<len; i++)
	{
		while(usart_flag_get(USART2, USART_TDBE_FLAG) == RESET);
		usart_data_transmit(USART2, data[i]);
	}
}

